import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

import 'normalize.css'
import '@/css/common.css'

Vue.config.productionTip = false

//import all svg
const requireAllSvg = requireContext => requireContext.keys().map(requireContext)
const req = require.context('./assets/svg', true, /\.svg$/)

requireAllSvg(req)

// 开发环境快捷 log
process.env.NODE_ENV === 'development' && (window.log = console.log.bind(console))

// 挂载 axios 服务
import service from '@/api'

window.axios = service

// 全局指令
const requireDirective = require.context('./directives', true, /\.js$/)

requireDirective.keys().forEach((filename) => {
	const directiveConfig = requireDirective(filename)
	const directive = directiveConfig.default || directiveConfig

	Vue.directive(filename.match(/\/(.*)\./)[1], directive)
})

// 全局注册组件
const requireComponent = require.context('./components/common', true, /\.vue$/)

requireComponent.keys().forEach((filename) => {
	const componentConfig = requireComponent(filename)
	const component = componentConfig.default || componentConfig

	Vue.component(component.name, component)
})

// 限制输入数字
Vue.directive('format', {
	bind: function (el, binding, vnode) {
		if (binding.modifiers.number) {
			el.addEventListener('input', () => {
				el.value = el.value.replace(/\D/g, '')

				// 处理组件绑定值
				vnode.context[binding.expression] = el.value
			}, false)
		}
	}
})

// 全局过滤
import {formatTime} from '@/utils'

Vue.filter('timeFormat', (value) => formatTime(value))

// toast
import Toast from '@/components/toast'

Vue.use(Toast)

import actionSheet from '@/components/action-sheet'

Vue.use(actionSheet)

// 时间格式化
import {timeToFormat} from '@/utils'

window.timeToFormat = timeToFormat

new Vue({
	router,
	store,
	render: h => h(App)
}).$mount('#app')